package com.ztesoft.sca.dao;

import com.ztesoft.sca.model.ImButtonDto;
import com.ztesoft.sca.model.ImMenuDto;
import com.ztesoft.sca.model.ImPageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author guojd
 */
@Component
public class ImMenuManageDtoImpl implements ImMenuManageDao {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public ImMenuManageDtoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 新增菜单ID
     */
    @Override
    public Long getNewMenuId() {
        String sqlStr = "select IM_MENU_SEQ.Nextval from dual";
        Long menuId = this.jdbcTemplate.queryForObject(sqlStr, new Object[]{}, Long.class);
        return menuId;
    }

    /**
     * 新增页面ID
     */
    @Override
    public Long getNewPageId() {
        String sqlStr = "select IM_PAGE_SEQ.Nextval from dual";
        Long pageId = this.jdbcTemplate.queryForObject(sqlStr, new Object[]{}, Long.class);
        return pageId;
    }

    /**
     * 新增菜单
     *
     * @param imMenuDto 菜单对象
     */
    @Override
    public void insertMenu(ImMenuDto imMenuDto) {
        Object[] param = new Object[]{imMenuDto.getMenuId(), imMenuDto.getMenuCode(), imMenuDto.getMenuName(), imMenuDto.getParentId()};
        String sqlBuf = "insert into IM_MEUN" +
                "  (MENU_ID,MENU_CODE,MENU_NAME,PARENT_ID)" +
                "VALUES" +
                "  (?,?,?,?)";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     */
    @Override
    public void deleteMenu(Long menuId) {
        Object[] param = new Object[]{menuId};
        String sqlBuf = "delete IM_MEUN where MENU_ID=?";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 修改菜单
     *
     * @param imMenuDto 菜单对象
     */
    @Override
    public void updateMenu(ImMenuDto imMenuDto) {
        Object[] param = new Object[]{imMenuDto.getMenuCode(), imMenuDto.getMenuName(), imMenuDto.getParentId(), imMenuDto.getMenuId()};
        String sqlBuf = "update IM_MEUN" +
                " set MENU_CODE=?,MENU_NAME=?,PARENT_ID=?" +
                " where MENU_ID=?";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 查询菜单
     *
     * @param param 查询参数
     *              menuName/menuId/parentId/menuCode
     * @return 菜单列表
     */
    @Override
    public List<ImMenuDto> selectMenus(Map<String, Object> param) {
        String sqlStr = "select * from IM_MEUN where 1=1";
        List<Object> params = new ArrayList<>();
        if (param.containsKey("menuName") && param.get("menuName").toString().length() > 0) {
            sqlStr += " and MENU_NAME like '%'||?||'%'";
            params.add(param.get("menuName"));
        }
        if (param.containsKey("menuId")) {
            sqlStr += " and MENU_ID=?";
            params.add(param.get("menuId"));
        }
        if (param.containsKey("parentId")) {
            if (param.get("parentId") == null) {
                sqlStr += " and PARENT_ID is null";
            } else {
                sqlStr += " and PARENT_ID=?";
                params.add(param.get("parentId"));
            }
        }
        if (param.containsKey("menuCode")) {
            sqlStr += " and MENU_CODE=?";
            params.add(param.get("menuCode"));
        }
        return this.jdbcTemplate.query(sqlStr, params.toArray(), BeanPropertyRowMapper.newInstance(ImMenuDto.class));
    }

    /**
     * 菜单列表树形
     *
     * @param parentId 父级ID，如果为空则查询全部
     * @return 菜单列表
     */
    @Override
    public List<ImMenuDto> selectMenuTree(Long parentId) {
        String sqlStr = "select * from IM_MEUN where 1=1";
        List<Object> params = new ArrayList<>();
        if (parentId != null) {
            sqlStr += " start with parent_id = ? connect by prior menu_id=parent_id";
            params.add(parentId);
        } else {
            sqlStr += " start with parent_id is null connect by prior menu_id=parent_id";
        }
        sqlStr += "  order siblings by menu_name";
        return this.jdbcTemplate.query(sqlStr, params.toArray(), BeanPropertyRowMapper.newInstance(ImMenuDto.class));
    }

    /**
     * 根据菜单ID查询按钮
     *
     * @param menuId 菜单ID
     * @return 按钮列表
     */
    @Override
    public List<ImButtonDto> selectButtonsByMenuId(Long menuId) {
        String sqlStr = "select b.* from IM_MENU_BUTTON_RELA r\n" +
                "left outer join IM_BUTTON b\n" +
                "on b.button_id=r.button_id where 1=1";
        List<Object> params = new ArrayList<>();
        if (menuId != null) {
            sqlStr += " and MENU_ID=?";
            params.add(menuId);
        }
        return this.jdbcTemplate.query(sqlStr, params.toArray(), BeanPropertyRowMapper.newInstance(ImButtonDto.class));
    }

    /**
     * 菜单按钮关系新增
     *
     * @param menuId   菜单ID
     * @param buttonId 按钮ID
     */
    @Override
    public void insertButtonMenuRela(Long menuId, Long buttonId) {
        Object[] param = new Object[]{menuId, buttonId};
        String sqlBuf = "insert into IM_MENU_BUTTON_RELA" +
                "  (MENU_ID,BUTTON_ID)" +
                "VALUES" +
                "  (?,?)";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 删除菜单按钮关系
     *
     * @param menuId   菜单ID
     * @param buttonId 按钮ID
     */
    @Override
    public void deleteButtonMenuRela(Long menuId, Long buttonId) {
        Object[] param = new Object[]{menuId, buttonId};
        String sqlBuf = "delete IM_MENU_BUTTON_RELA" +
                "  where MENU_ID=? and BUTTON_ID=?";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 新增页面
     *
     * @param imPageDto 页面对象
     */
    @Override
    public void insertPage(ImPageDto imPageDto) {
        Object[] param = new Object[]{imPageDto.getPageId(), imPageDto.getMenuId(), imPageDto.getPageCode(), imPageDto.getPageName(), imPageDto.getTitle(), imPageDto.getUrl()};
        String sqlBuf = "insert into IM_PAGE" +
                "  (PAGE_ID,MENU_ID,PAGE_CODE,PAGE_NAME,TITLE,URL)" +
                "VALUES" +
                "  (?,?,?,?,?,?)";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 删除页面
     *
     * @param pageId 页面ID
     */
    @Override
    public void deletePage(Long pageId) {
        Object[] param = new Object[]{pageId};
        String sqlBuf = "delete IM_PAGE" +
                "  where PAGE_ID=?";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 更新页面
     *
     * @param imPageDto 页面对象
     */
    @Override
    public void updatePage(ImPageDto imPageDto) {
        Object[] param = new Object[]{imPageDto.getMenuId(), imPageDto.getPageCode(), imPageDto.getPageName(), imPageDto.getTitle(), imPageDto.getUrl(), imPageDto.getPageId()};
        String sqlBuf = "update IM_PAGE" +
                "  set MENU_ID=?,PAGE_CODE=?,PAGE_NAME=?,TITLE=?,URL=?" +
                " where PAGE_ID=?";
        this.jdbcTemplate.update(sqlBuf, param);
    }

    /**
     * 查询页面列表
     *
     * @param param 参数
     *              pageId/menuId/pageCode/pageName/title/url
     * @return 页面列表
     */
    @Override
    public List<ImPageDto> selectPages(Map<String, Object> param) {
        String sqlStr = "select * from IM_PAGE where 1=1";
        List<Object> params = new ArrayList<>();
        if (param.containsKey("pageName")) {
            sqlStr += " and PAGE_NAME like '%'||?||'%'";
            params.add(param.get("pageName"));
        }
        if (param.containsKey("title")) {
            sqlStr += " and TITLE like '%'||?||'%'";
            params.add(param.get("title"));
        }
        if (param.containsKey("url")) {
            sqlStr += " and URL like '%'||?||'%'";
            params.add(param.get("url"));
        }
        if (param.containsKey("pageId")) {
            sqlStr += " and PAGE_ID=?";
            params.add(param.get("pageId"));
        }
        if (param.containsKey("menuId")) {
            sqlStr += " and MENU_ID=?";
            params.add(param.get("menuId"));
        }
        if (param.containsKey("pageCode")) {
            sqlStr += " and PAGE_CODE=?";
            params.add(param.get("pageCode"));
        }
        return this.jdbcTemplate.query(sqlStr, params.toArray(), BeanPropertyRowMapper.newInstance(ImPageDto.class));
    }
}
